package com.atguigu.gmall.common.config.minio.config;

import com.atguigu.gmall.common.config.minio.properties.MinioProperties;
import io.minio.MinioClient;
import io.minio.errors.InvalidEndpointException;
import io.minio.errors.InvalidPortException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class MinioConfiguration {

    @Autowired
    MinioProperties minioProperties;
    @Bean
    public MinioClient minioClient() throws Exception {

        //1.创建MinioClient对象
        MinioClient minioClient = new MinioClient(minioProperties.getEndpoint(),
                minioProperties.getAccessKey(),
                minioProperties.getSecretKey());
        //2.判断桶是否存在
        boolean bucketExists = minioClient.bucketExists(minioProperties.getBucketName());
        if (!bucketExists){
            minioClient.makeBucket(minioProperties.getBucketName());
        }
        return minioClient;
    }
}
